﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{
    public class UserMethod:IDisposable
    {
        private ToDoListDataContext _ctx = new ToDoListDataContext();

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool SignIn(int userId, string passWord)
        {
            var user = _ctx.UsersData.SingleOrDefault(Id => Id.UserId == userId);
            var pass = _ctx.UsersData.Where(Id => Id.UserId == userId).SingleOrDefault(pw => pw.Password.Equals(passWord));
            if (user == null || pass == null)
            {
                return false;
            }
            else
                return true;
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="passWord"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int Register(string passWord, string userName)
        {
            UsersData user = new UsersData()
            {
                UserName = userName,
                Password = passWord
            };
            _ctx.UsersData.Add(user);
            _ctx.SaveChanges();
            return user.UserId;
        }
        /// <summary>
        /// 未完成事件统计
        /// </summary>
        /// <param name="userId"></param>
        public List<ItemsData> IncompleteEvent(int userId)
        {
            var WaitToDoLists = _ctx.ItemsData.Where(Id => Id.UserId == userId)
                .Where(state => state.State == false).ToList();
            return WaitToDoLists;
        }
        /// <summary>
        /// 所有事件浏览
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ItemsData> AllEventQueries(int userId)
        {
            var items = _ctx.ItemsData.Where(Id => Id.UserId == userId).ToList();
            if (items != null)
            {
                return items;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 过期事件提醒
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ItemsData> OverdueItems(int userId)
        {
            var items = _ctx.ItemsData.Where(Id => Id.UserId == userId)
                .Where(Time => DateTime.Now > Time.OverdueEvent).ToList();
            if (items != null)
            {
                return items;
            }
            else
            {
                return null;
            }
        }
        public void Dispose()
        {
            _ctx.Dispose();
        }
    }
}
